package com.baseframe.topic.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.topic.entity.SnsTestTopic;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baseframe.topic.entity.SnsTestTopicItem;
import com.baseframe.topic.entity.SnsTestTopicItemOption;
import com.baseframe.topic.entity.request.SnsTestTopicRequest;
import com.baseframe.topic.entity.vo.SnsTestTopicItemOptionVO;
import com.baseframe.topic.entity.vo.SnsTestTopicItemVO;
import com.baseframe.topic.entity.vo.SnsTestTopicVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**

* @description 针对表【sns_test_topic】的数据库操作Mapper
* @createDate 2023-06-29 23:12:25
* @Entity com.baseframe.topic.entity.SnsTestTopic
*/
public interface SnsTestTopicMapper extends BaseMapper<SnsTestTopic> {

    @Select("select stt.*,(select SUM(stti.award) as total_score from sns_test_topic_item as stti where is_deleted = 0 and stti.topic_id = stt.id) as total_score,(select sttur.result from sns_test_topic_user_result as sttur where sttur.is_deleted = 0 and sttur.user_id = #{request.userId} and sttur.topic_id = stt.id) as result from sns_test_topic as stt where is_deleted = 0 order by stt.create_time desc ")

    Page<SnsTestTopicVO> getTestTopicPageList(Page<SnsTestTopicVO> page,@Param("request") SnsTestTopicRequest request);

    @Select("select stt.*,(select SUM(stti.award) as total_score from sns_test_topic_item as stti where is_deleted = 0 and stti.topic_id = stt.id) as total_score from sns_test_topic as stt where is_deleted = 0 and stt.id = #{request.id}")
    @Results(
        {
            @Result(
                    column = "id",
                    property = "id",
                    id = true
            ),
            @Result(
                    column = "id",
                    property = "topicItemList",
                    javaType = List.class,
                    many = @Many(select = "getTestTopicItems")
            )
        }
    )
    SnsTestTopicVO getTestTopicDetails(@Param("request") SnsTestTopicRequest request);


    /**
     * 通过能测id获取能测题目列表
     * @param topicId
     * @return
     */
    @Select("select id, topic_id, name, award, sort from sns_test_topic_item where is_deleted = 0 and topic_id = #{topicId} order by sort")
    @Results(
        {
            @Result(
                    column = "id",
                    property = "id",
                    id = true
            ),
            @Result(
                    property = "optionList",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "getTestTopicItemOptions")
            )
        }
    )
    List<SnsTestTopicItemVO> getTestTopicItems(@Param("topicId") Long topicId);


    /**
     * 获取能测选项列表
     * @param topicItemId
     * @return
     */
    @Select("select id, topic_item_id, name, value from sns_test_topic_item_option where is_deleted = 0 and topic_item_id = #{topicItemId}")
    List<SnsTestTopicItemOptionVO> getTestTopicItemOptions(@Param("topicItemId") Long topicItemId);
}




